package com.canaan.business.controller.pc.bill;

import com.canaan.business.domain.bill.ScrapProductBill;
import com.canaan.business.domain.vo.ScrapProductBillExportVo;
import com.canaan.business.domain.vo.ScrapProductBillVo;
import com.canaan.business.service.bill.IScrapProductBillService;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.core.page.TableDataInfo;
import com.canaan.common.enums.BusinessType;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 报废单Controller
 *
 * @author ruoyi
 * @date 2023-01-31
 */
@Api(tags = "报废单")
@RestController
@RequestMapping("/bill/scrap")
public class ScrapProductBillController extends BaseController {
    @Resource
    private IScrapProductBillService scrapProductBillService;

    /**
     * 查询报废单列表
     */
    @ApiOperation(value = "查询报废单列表")
    @GetMapping("/list")
    public TableDataInfo list(ScrapProductBill scrapProductBill) {
        startPage();
        List<ScrapProductBill> list = scrapProductBillService.selectScrapProductBillList(scrapProductBill);
        return getDataTable(list);
    }

    /**
     * 导出报废单列表
     */
    @ApiOperation(value = "导出报废单列表")
    @Log(title = "报废单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScrapProductBill scrapProductBill) {
        List<ScrapProductBillExportVo> list = scrapProductBillService.exportList(scrapProductBill);
        ExcelUtil<ScrapProductBillExportVo> util = new ExcelUtil<>(ScrapProductBillExportVo.class);
        util.exportExcel(response, list, "报废单数据");
    }

    /**
     * 获取报废单详细信息
     */
    @ApiOperation(value = "获取报废单详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(scrapProductBillService.selectScrapProductBillById(id));
    }

    /**
     * 新增报废单
     */
    @ApiOperation(value = "新增报废单")
    @Log(title = "报废单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScrapProductBill scrapProductBill) {
        return toAjax(scrapProductBillService.insertScrapProductBill(scrapProductBill));
    }

    /**
     * 修改报废单
     */
    @ApiOperation(value = "修改报废单")
    @Log(title = "报废单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScrapProductBill scrapProductBill) {
        return toAjax(scrapProductBillService.updateScrapProductBill(scrapProductBill));
    }

    /**
     * 删除报废单
     */
    @ApiOperation(value = "删除报废单")
    @Log(title = "报废单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(scrapProductBillService.deleteScrapProductBillByIds(ids));
    }


    /**
     * 新增报废单
     */
    @ApiOperation(value = "新增报废单")
    @Log(title = "新增报废单", businessType = BusinessType.INSERT)
    @PostMapping("/addWithDetail")
    public AjaxResult addWithDetail(@RequestBody ScrapProductBillVo scrapProductBillVo) {
        return toAjax(scrapProductBillService.addWithDetail(scrapProductBillVo));
    }

    /**
     * 报废单导入模板
     */
    @ApiOperation(value = "报废单导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ScrapProductBillExportVo> util = new ExcelUtil<>(ScrapProductBillExportVo.class);
        util.importTemplateExcel(response, "报废单导入模板");
    }

    /**
     * 订单取消
     */
    @ApiOperation(value = "订单取消")
    @Log(title = "订单取消", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel")
    public AjaxResult cancel(@RequestBody List<Long> ids) {
        return success(scrapProductBillService.cancel(ids));
    }

    /**
     * 订单确认取消
     */
    @ApiOperation(value = "订单确认取消")
    @Log(title = "订单取消", businessType = BusinessType.UPDATE)
    @PutMapping("/confirmCancel")
    public AjaxResult cancel(@RequestBody ScrapProductBill scrapProductBill) {
        if(scrapProductBill == null || scrapProductBill.getId() == null){
            throw new ServiceException("缺少必要参数");
        }
        return success(scrapProductBillService.confirmCancel(scrapProductBill));
    }
}
